package com.qzimp.fujiancuo.service.impl;

import ch.qos.logback.core.status.Status;
import com.j256.simplemagic.ContentInfo;
import com.j256.simplemagic.ContentInfoUtil;
import com.qzimp.fujiancuo.config.MinioConfig;
import com.qzimp.fujiancuo.entity.MinioFile;
import com.qzimp.fujiancuo.entity.vo.ResultCode;
import com.qzimp.fujiancuo.entity.vo.ResultVo;
import com.qzimp.fujiancuo.entity.vo.StatusCode;
import com.qzimp.fujiancuo.mapper.MinioFileMapper;
import com.qzimp.fujiancuo.service.HuanghansenService;
import io.minio.MinioClient;
import io.minio.UploadObjectArgs;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;

@Service
public class HuanghansenServiceImpl implements HuanghansenService {

    @Resource
    MinioFileMapper minioFileMapper;
    @Resource
    MinioClient minioClient;
    @Value("${minio.endpoint}")
    private String endpoint;
    @Value("${minio.accessKey}")
    private String accessKey;
    @Value("${minio.secretKey}")
    private String secretKey;



    @Override
    public MinioFile uploadFile(String md5, String fileName, String filePath) {
        MinioFile file = minioFileMapper.selectOneById(md5);
        if (file != null) {
            return file;
        }

        MinioFile mediaFiles  = insertMinioAndDB(md5, fileName, filePath);
        return mediaFiles;
    }

    private MinioFile insertMinioAndDB(String md5, String fileName, String filePath) {
        try {
            insertMinio(md5, fileName, filePath);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

        MinioFile minioFile = insertDB(md5, fileName, filePath);
        return minioFile;
    }

    private MinioFile insertDB(String md5, String fileName, String filePath) {
        String fileExt = fileName.substring(fileName.lastIndexOf("."));
        String contentType = getContentType(fileExt);

        MinioFile minioFile = new MinioFile();
        minioFile.setId(Long.valueOf(md5));
        minioFile.setFilePath("cuo/"+getFilePathByMd5(md5, fileExt));
        minioFile.setDomain(endpoint);
        minioFile.setName(fileName);
        minioFile.setOriginalName(fileName);
        minioFile.setHash(md5);
        minioFile.setPutTime(LocalDateTime.now());
        minioFile.setContentType(contentType);

        int insert = minioFileMapper.insert(minioFile);
        if (insert > 0) {
            return minioFile;
        }else {
            return null;
        }
    }

    private void insertMinio(String md5, String fileName, String filePath) throws IOException {
        String fileExt = fileName.substring(fileName.lastIndexOf("."));
        String contentType = getContentType(fileExt);

        UploadObjectArgs uploadObjectArgs = UploadObjectArgs.builder()
                .filename(filePath)
                .bucket("cuo")
                .object(getFilePathByMd5(md5, fileExt))
                .contentType(contentType)
                .build();
        try {
            minioClient.uploadObject(uploadObjectArgs);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private String getFilePathByMd5(String fileMd5,String fileExt){
        return   fileMd5.substring(0,1) + "/" + fileMd5.substring(1,2) + "/" + fileMd5 + "/" +fileMd5 +fileExt;
    }
    private String getContentType(String extension) {
        if(extension == null){
            extension = "";
        }
        //根据扩展名取出mimeType
        String mimeType = MediaType.APPLICATION_OCTET_STREAM_VALUE;//通用mimeType，字节流
        ContentInfo extensionMatch = ContentInfoUtil.findExtensionMatch(extension);
        if(extensionMatch!=null){
            mimeType = extensionMatch.getMimeType();
        }
        return mimeType;
    }
}
